/*
 *  Copyright 2012.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.queryhandler.n.client.services;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import org.opu.queryhandler.n.client.services.beans.DataSource;
import org.opu.queryhandler.n.util.ConfigPath;
import org.opu.queryhandler.n.util.Logger;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.core.Persister;

/**
 *
 */
public class ConnectionService {
    private Logger logger = Logger.getInstance();

    public void updeteDataSource(Collection<DataSource> list) throws IOException{
        logger.info(this, "Update data source to " + list);
        File file = ConfigPath.getInstance().getUserDbProp();
        writeList(new HashSet<DataSource>(list), file);
    }

    public Collection<DataSource> getSavedDataSource() throws IOException{
        File file = ConfigPath.getInstance().getUserDbProp();
        
        if(!file.exists()){
            return Collections.emptyList();
        }
        
        HashSet<DataSource> list = readList(file);

        return list;
    }

    public void addDataSource(DataSource ds) throws IOException{
        HashSet<DataSource> list;

        File file = ConfigPath.getInstance().getUserDbProp();

        if(file.exists()){
            list = readList(file);
        } else {
            list = new HashSet<DataSource>();
        }

        list.add(ds);

        writeList(list, file);
    }

    private void writeList(HashSet<DataSource> list, File file) throws IOException {
        try {
            new Persister().write(new DataSources(list), file);
        } catch (Exception ex) {
            throw new IOException(ex);
        }
    }

    private HashSet<DataSource> readList(File file) throws IOException  {
        try {
            return new Persister().read(DataSources.class, file).list;
        } catch (Exception ex) {
            try {
                file.delete();
            } catch (Exception e) {
                logger.error(this, "Can't delete broken file with connection list");
                logger.error(this, e);
            }
            throw new IOException(ex);
        }
    }

    @Root(name="connections") public static class DataSources {
        @ElementList public HashSet<DataSource> list;

        public DataSources(HashSet<DataSource> list) {
            this.list = list;
        }

        public DataSources() {
        }
    }
}
